package com.ruoyi.project.erp.goods.controller;

import java.util.List;

import cn.hutool.dfa.WordTree;
import com.ruoyi.common.utils.bean.EmptyUtil;
import com.ruoyi.framework.web.domain.ZtreeString;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.goods.domain.ErpGoods;
import com.ruoyi.project.erp.goods.service.IErpGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.domain.Ztree;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物品Controller
 *
 * @author qinghuan
 * @date 2021-08-08
 */
@Controller
@RequestMapping("/erp/goods")
public class ErpGoodsController extends BaseController {
    private String prefix = "erp/goods";

    @Autowired
    private IErpGoodsService erpGoodsService;

    @RequiresPermissions("erp:goods:view")
    @GetMapping()
    public String goods() {
        return prefix + "/goods";
    }

    /**
     * 查询物品树列表
     */
    @RequiresPermissions("erp:goods:list")
    @PostMapping("/list")
    @ResponseBody
    public List<ErpGoods> list(ErpGoods erpGoods) {
        List<ErpGoods> list = erpGoodsService.selectErpGoodsList(erpGoods);
        return list;
    }

    /**
     * 导出物品列表
     */
    @RequiresPermissions("erp:goods:export")
    @Log(title = "物品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ErpGoods erpGoods) {
        List<ErpGoods> list = erpGoodsService.exportErpGoodsList(erpGoods);
        ExcelUtil<ErpGoods> util = new ExcelUtil<ErpGoods>(ErpGoods.class);
        return util.exportExcel(list, "物品数据");
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("erp:goods:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ErpGoods> util = new ExcelUtil<ErpGoods>(ErpGoods.class);
        List<ErpGoods> goodsList = util.importExcel(file.getInputStream());
//        erpGoodsService.importGoods();
        String message = erpGoodsService.importGoods(goodsList, updateSupport);
        return AjaxResult.success(message);
    }

    @RequiresPermissions("erp:goods:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ErpGoods> util = new ExcelUtil<ErpGoods>(ErpGoods.class);
        return util.importTemplateExcel("明细数据");
    }


    /**
     * 新增物品
     */
    @GetMapping(value = {"/add/{goodsId}", "/add/"})
    public String add(@PathVariable(value = "goodsId", required = false) String goodsId, ModelMap mmap) {
        if (StringUtils.isNotNull(goodsId)) {
            mmap.put("erpGoods", erpGoodsService.selectErpGoodsByGoodsId(goodsId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存物品
     */
    @RequiresPermissions("erp:goods:add")
    @Log(title = "物品", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ErpGoods erpGoods) {
        if (EmptyUtil.isEmpty(erpGoods.getGoodsId())) {
            return error("物品编码不能为空");
        }
        if (StringUtils.isContainChinese(erpGoods.getGoodsId())) {
            return error("物品编码不能包含中文");
        }
        if (erpGoods.getGoodsId().contains("/")) {
            return error("物品编码不能包含/");
        }
        if (erpGoods.getGoodsId().contains("\n")) {
            return error("物品编码不能包含换行符");
        }



        return toAjax(erpGoodsService.insertErpGoods(erpGoods));
    }

    /**
     * 修改物品
     */
    @GetMapping("/edit/{goodsId}")
    public String edit(@PathVariable("goodsId") String goodsId, ModelMap mmap) {
        ErpGoods erpGoods = erpGoodsService.selectErpGoodsByGoodsId(goodsId);
        mmap.put("erpGoods", erpGoods);
        return prefix + "/edit";
    }

    /**
     * 修改保存物品
     */
    @RequiresPermissions("erp:goods:edit")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ErpGoods erpGoods) {
        return toAjax(erpGoodsService.updateErpGoods(erpGoods));
    }

    /**
     * 删除
     */
    @RequiresPermissions("erp:goods:remove")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{goodsId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("goodsId") String goodsId) {
        return toAjax(erpGoodsService.deleteErpGoodsByGoodsId(goodsId));
    }

    /**
     * 删除
     */
    @RequiresPermissions("erp:goods:remove")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeAll(String ids) {
        return toAjax(erpGoodsService.deleteErpGoodsByGoodsIds(ids));
    }


    /**
     * 通过项目删除物品
     */
    @RequiresPermissions("erp:goods:remove")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @GetMapping("/removeAllByProjectId/{projectId}")
    @ResponseBody
    public AjaxResult removeAllByProjectId(@PathVariable("projectId") Long projectId) {
        return toAjax(erpGoodsService.removeAllByProjectId(projectId));
    }


    /**
     * 选择物品树
     */
    @GetMapping(value = {"/selectGoodsTree/{goodsId}", "/selectGoodsTree/"})
    public String selectGoodsTree(@PathVariable(value = "goodsId", required = false) String goodsId, ModelMap mmap) {
        if (StringUtils.isNotNull(goodsId)) {
            mmap.put("erpGoods", erpGoodsService.selectErpGoodsByGoodsId(goodsId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载物品树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<ZtreeString> treeData() {
        List<ZtreeString> ztrees = erpGoodsService.selectErpGoodsTree();
        return ztrees;
    }
}
